<template>
	<view class="reply-container">
		<view style="padding: 20rpx;">
			<view class="title"><text style="color: red;">*</text>整改结果</view>
			<textarea class="content" v-model="params.description" placeholder="请输入整改结果..." name="" id="" cols="30" rows="10"></textarea>
		</view>
		<view class="title" style="margin-left: 20rpx;"><text style="color: red;">*</text>整改附件</view>
		<view style="margin-left: 8rpx;">
			<upload @input='onchange'></upload>
		</view>
		<view class="title" style="margin-left: 20rpx;">责任人签字</view>
		<view class="sign-item" @click="goSignPage">
			<image :src="resultUrl" mode=""></image>
		</view>
		<!-- <view class="sign-img">
			<view class="left-text">业主签字:</view>
			<view class="sign-item" @click="goSignPage">
				<image :src="resultUrl" mode=""></image>
			</view>
		</view> -->
		<button class="grid-btn" @click="submit">提交</button>
	</view>
</template>

<script>
	import upload from '../components/upload.vue'
	import {
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				params: {
					id: null,
					opType: 'submit',
					description: '',
					accessory: [],
					signature: ''
				},
				resultUrl: '../static/icon/8.png',
				file: {
					fileName: null,
					fileData: null
				}
			}
		},
		components: {
			upload
		},
		onShow() {
			try {
				if (uni.getStorageSync('ceshiUrl')) {
					this.resultUrl = uni.getStorageSync('ceshiUrl');
					uni.removeStorageSync('ceshiUrl');
				}

			} catch (e) {
				// error
			}
		},
		onLoad(options) {
			this.params.id = +options.id
		},
		methods: {
			goSignPage() {
				uni.navigateTo({
					url: '../sign/sign'
				})
			},
			onchange(e) {
				this.params.accessory = e
			},
			submit() {
				if (this.params.description.trim() == '') {
					uni.showToast({
						title: '请填写整改结果',
						icon: 'none'
					})
				} else if (this.params.accessory.length === 0) {
					uni.showToast({
						title: '请上传整改附件',
						icon: 'none'
					})
				} else {
					uni.showLoading({
						mask: true
					})
					if (this.resultUrl !== '../static/icon/8.png') {
						// this.urlTobase64(this.resultUrl)
						this.uploadSign()
					} else {
						this.completeRectify()
					}
				}
			},
			async uploadSign() {
				this.file = await this.urlTobase64(this.resultUrl)
				this.params.signature = await this.uploadFile()
				const res = await this.$http.post('gridSystem/check/solve', this.params)
				if (res.code === 10000) {
					uni.redirectTo({
						url: '/fireGridCheck/acceptSuccess/acceptSuccess?path=../rectifyTask/rectifyTask' + '&title=返回任务列表'
					})
				}
			},
			urlTobase64(url) {
				return new Promise((resolve, reject) => {
					uni.getFileSystemManager().readFile({
						filePath: url, //选择图片返回的相对路径
						encoding: 'base64', //编码格式
						success: res => { //成功的回调
							let base64 = 'data:image/jpeg;base64,' + res.data //不加上这串字符，在页面无法显示的哦
							// this.dataList.picList.push(base64);
							console.log(base64)
							let file = {
								fileData: base64,
								fileName: url
							}
							resolve(file)
						}
					})
				})
			},
			// 上传文件
			uploadFile() {
				return new Promise((resolve, reject) => {
					this.$http.post('common/file/upload', this.file).then(res => {
						console.log(res)
						if (res.code === 10000) {
							// this.baseArr.push(res.data.id)
							resolve(res.data.id)
							// this.params.业主签字 = res.data.id
							// this.completeRectify()
						}
					})
				})

			},
			completeRectify() {
				// this.params.id = parseInt(this.ticketId)
				// this.params.操作类型 = 'complete'
				uni.showLoading({
					mask: true
				})
				this.$http.post('gridSystem/check/solve', this.params).then(res => {
					console.log(res)
					if (res.code === 10000) {
						uni.redirectTo({
							url: '/fireGridCheck/acceptSuccess/acceptSuccess?path=../rectifyTask/rectifyTask' + '&title=返回任务列表'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.reply-container {

		// padding: 20rpx;
		.title {
			margin-bottom: 20rpx;
		}

		.content {
			width: 710rpx;
			padding: 20rpx;
			border: 2rpx solid #ccc;
		}

		.btn {
			width: 568rpx;
			height: 80rpx;
			line-height: 80rpx;
			margin-top: 100rpx;
			background-color: #2E85FB;
			color: #fff;
		}

		.sign-item {
			margin-left: 20rpx;
			width: 220rpx;
			height: 220rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
